% CHANNEL EQUALIZATION USING LMS
clc;
clear all;
close all;
format long;

rand('seed', 134);
<<<<<<< HEAD
randn('seed', 134);
=======
>>>>>>> d0274871dc5db9d9864240ff5fbea611b1e64a05
M=3000;    % number of data samples
T=2000;    % number of training symbols
dB=25;     % SNR in dB value

L=12; % length for smoothing(L+1)
ChL=10;  % length of the channel(ChL+1), 即多径个数
EqD=round((L+ChL)/2);  % delay for equalization

% Ch=randn(1,ChL+1)+sqrt(-1)*randn(1,ChL+1);   % complex channel
Ch = [0.0410+0.0109j, 0.0495+0.0123j, 0.0672+0.0170j, 0.0919+0.0235j,0.7920+0.1281j, 0.3960+0.0871j, 0.2715+0.0498j, 0.2291+0.0414j, 0.1287+0.0154j,0.1032+0.0119j];
Ch=Ch/norm(Ch);                     % scale the channel with norm

TxS=round(rand(1,M))*2-1;
TxS=TxS+sqrt(-1)*(round(rand(1,M))*2-1);   %3000 个 qpsk信号

x=filter(Ch,1,TxS);  %channel distortion
n=randn(1,M);  %+sqrt(-1)*randn(1,M);   %Additive white gaussian noise
n=n/norm(n)*10^(-dB/20)*norm(x);  % scale the noise power in accordance with SNR
x=x+n;                           % 通过信道后的3000个接收符号




K=M-L;   %% Discarding several starting samples for avoiding 0's and negative
X=zeros(L+1,K);  % 每一列都是一个sample   31 * 2970
for i=1:K
    X(:,i)=x(i+L:-1:i).';
end

%adaptive RLS Equalizer
e=zeros(1,T-10);  % initial error
c=zeros(1, L+1);   % 初始权值，就是31维的向量

<<<<<<< HEAD
R_inverse = 100*eye(L+1);  % 31 * 31

%disp(R_inverse)
for k=1:1:1
    % X(:,k+10)
    % TxS(k+10+L-EqD)
    e(k) = TxS(k+10+L-EqD) - c*X(:,k+10);
    % disp(e(k))
    filtered_infrmn_vect=R_inverse*X(:,k+10);
    norm_error_power=X(:,k+10)'*filtered_infrmn_vect;
    X(:,k+10)'
    norm_error_power
    gain_constant=1/(1+norm_error_power);

    norm_filtered_infrmn_vect=gain_constant*filtered_infrmn_vect';
    % norm_filtered_infrmn_vect
    % updating weight values of equalizer
    % weight update using rls
    c=c+(e(k)*norm_filtered_infrmn_vect);
    %disp(c)
    R_inverse=R_inverse-norm_filtered_infrmn_vect'*norm_filtered_infrmn_vect;
    % disp(R_inverse)
    % break
end

sb=c*X;   % recieved symbol estimation

%SER(decision part)
sb1=sb/norm(c);  % normalize the output
sb1=sign(real(sb1))+sqrt(-1)*sign(imag(sb1));  %symbol detection
start=12;
sb2=sb1-TxS(start+1:start+length(sb1));  % error detection
SER=length(find(sb2~=0))/length(sb2); %  SER calculation
disp(SER);

R_inverse = 100*eye(L+1); % 31 * 31

dlmwrite('data_Tx.txt', TxS, 'delimiter', '\t'); % 发送数据,作为reference
dlmwrite('data_y.txt', X, 'delimiter', '\t'); % receive
=======
% Initialization of Rinverse
R_inverse = 100*eye(L+1); % 31 * 31

dlmwrite('/home/lee/data_Tx.txt', TxS, 'delimiter', '\t'); % 发送数据,作为reference
dlmwrite('/home/lee/data_y.txt', X, 'delimiter', '\t'); % receive
% for k=1:1:1
%     e(k) = TxS(k+10+L-EqD) - c*X(:,k+10);
%     filtered_infrmn_vect=R_inverse*X(:,k+10);
%     disp(filtered_infrmn_vect)
%     disp('Text');
%     norm_error_power=X(:,k+10)'*filtered_infrmn_vect;
%     disp(norm_error_power);
%     %disp(norm_error_power);

%     gain_constant=1/(1+norm_error_power);
%     norm_filtered_infrmn_vect=gain_constant*filtered_infrmn_vect';

%     % updating weight values of equalizer
%     % weight update using rls
%     c=c+(e(k)*norm_filtered_infrmn_vect);
%     R_inverse=R_inverse-norm_filtered_infrmn_vect'*norm_filtered_infrmn_vect;
% end

% sb=c*X;   % recieved symbol estimation

% %SER(decision part)
% sb1=sb/norm(c);  % normalize the output
% sb1=sign(real(sb1))+sqrt(-1)*sign(imag(sb1));  %symbol detection
% start=12;
% sb2=sb1-TxS(start+1:start+length(sb1));  % error detection
% SER=length(find(sb2~=0))/length(sb2); %  SER calculation
% disp(SER);

% % plot of transmitted symbols
%     subplot(2,2,1),
%     plot(TxS,'*');
%     grid,title('Input symbols');  xlabel('real part'),ylabel('imaginary part')
%     axis([-2 2 -2 2])

% % plot of received symbols
%     subplot(2,2,2),
%     plot(x,'o');
%     grid, title('Received samples');  xlabel('real part'), ylabel('imaginary part')

% % plots of the equalized symbols
%     subplot(2,2,3),
%     plot(sb,'o');
%     grid, title('Equalized symbols'), xlabel('real part'), ylabel('imaginary part')

% % convergence
%     subplot(2,2,4),
%     plot(abs(e));
%     grid, title('Convergence'), xlabel('n'), ylabel('error signal')
%     %%
% print -depsc test2.eps
>>>>>>> d0274871dc5db9d9864240ff5fbea611b1e64a05
